//
//  HotBrandTableViewCell.m
//  19-Homework
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "HotBrandTableViewCell.h"
#import "HotBrandCollectionViewCell.h"
#import "HotBrand.h"
#define kCollectionViewReuse @"collReuse"
@interface HotBrandTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *myCollectionView;
@property (nonatomic, strong) NSMutableArray<HotBrand *> *hotBrandArray;
@end
@implementation HotBrandTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.hotBrandArray = [NSMutableArray array];
        [self creatCollectionView];
        [self getDataFromJSON];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _myCollectionView.frame = self.contentView.frame;
}

- (void)getDataFromJSON {
    NSURL *url = [NSURL URLWithString:@"http://223.99.255.20/cars.app.autohome.com.cn/dealer_v6.0.0/dealer/hotbrands-pm2.json"];
    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
    mutableRequest.HTTPMethod = @"GET";
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
        NSDictionary *dicJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *resultDic = [dicJSON objectForKey:@"result"];
        NSArray *listArray = [resultDic objectForKey:@"list"];
        for (NSDictionary *dictionary in listArray) {
            HotBrand *hotBrand = [HotBrand hotBrandWithDictionary:dictionary];
            [_hotBrandArray addObject:hotBrand];
        }
            [_myCollectionView reloadData];
        });

    }];
    [dataTask resume];
}

- (void)creatCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.contentView.frame) - 50) / 5, CGRectGetHeight(self.contentView.frame));
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    [self.contentView addSubview:_myCollectionView];
    [self.myCollectionView registerNib:[UINib nibWithNibName:@"HotBrandCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionViewReuse];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _hotBrandArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotBrandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.hotBrand = _hotBrandArray[indexPath.row];
    return cell;
}

@end
